﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessLogicLayer.Base;
using DALLayer.Providers;
using DataDefinition;
using Objects;

namespace BusinessLogicLayer.Manager
{
    public class VehicleModelManager : ManagerBase<VehicleModelDataProvider>
    {
         #region [Constructor]
         /// <summary>
        /// Initializes a new instance of the <see cref="VehicleModelManager" /> class.
        /// </summary>
        public VehicleModelManager() { }
        #endregion

        #region [Manager base class implementations]
        /// <summary>
        /// Pass the data provider to the class
        /// </summary>
        protected override void InitializeDataProvider()
        {
            //Pass the specific data provider to the manager class
            base.DataProvider = new VehicleModelDataProvider();
        }

        #endregion

        #region [Public Methods]
        /// <summary>
        /// Update or insert a vehicle model.
        /// </summary>
        /// <param name="vehicle">vehicle object</param>
        /// <returns>true or false</returns>
        public bool UpdateVehicleModel(VehicleModel vehicleModel)
        {
            bool success = false;
            try
            {
                //if there is an id
                if (vehicleModel.VehicleModelID > 0)
                {
                    //Update the vehicle model
                    success = base.DataProvider.UpdateVehicleModel(vehicleModel.VehicleModelID, vehicleModel.VehicleModelName, vehicleModel.IsActive);

                }
                else
                {
                    //Insert an vehicle model
                    success = base.DataProvider.InsertVehicleModel(vehicleModel.VehicleModelName, vehicleModel.IsActive);

                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            //returns true or false
            return success;

        }

        /// <summary>
        /// insert a vehicle model returns added record object.
        /// </summary>
        /// <param name="vehicle">vehicle object</param>
        /// <returns>vehicle models</returns>
        public List<VehicleModel> UpdateVehicleModelScalar(VehicleModel vehicleModel)
        {
            List<VehicleModel> vehicleModels = null;
            try
            {
                int identity = base.DataProvider.InsertVehicleModelScalar(vehicleModel.VehicleModelName, vehicleModel.IsActive);
                //Insert an vehicle model
                AutoFixDataSet dataSet = base.DataProvider.GetVehicleModelDetails(identity, true);
                if (dataSet != null && dataSet.VehicleModel != null && dataSet.VehicleModel.Rows != null)
                {
                    vehicleModels = FillVehicleModelObjectsByEntities(dataSet.VehicleModel);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            //returns vehicleModels
            return vehicleModels;

        }

        /// <summary>
        /// Gets all the vehicle models
        /// </summary>
        /// <returns>vehicle model list</returns>
        public List<VehicleModel> GetVehicleModels()
        {
            //The vehicle model details
            List<VehicleModel> vehicleModelDetails = null;
            try
            {
                //Get the vehicle model details list
                vehicleModelDetails = this.GetVehicleModels(0, false);
            }
            catch (Exception eX)
            {
                //log error here
            }
            //The vehicle model details list to return
            return vehicleModelDetails;
        }
        /// <summary>
        /// Gets vehicle models by id
        /// </summary>
        /// <param name="vehicleModelID">vehicle model id</param>
        /// <returns>vehicleModelDetails</returns>
        public List<VehicleModel> GetVehicleModelsByID(int vehicleModelID)
        {
            //The vehicle model details
            List<VehicleModel> vehicleModelDetails = null;
            try
            {
                //Get the vehicle model details list bt id
                vehicleModelDetails = this.GetVehicleModels(vehicleModelID, true);
            }
            catch (Exception eX)
            {
                //log error here
            }
            //The vehicle model details list to return
            return vehicleModelDetails;
        }


        /// <summary>
        /// Gets all the vehicle models
        /// </summary>
        /// <returns>the vehicle model vehicle model id list</returns>
        public List<VehicleModelsVehicleModelID> VehicleModelDetailsByVehicleModelID()
        {
            //The vehicle model details to be returned
            List<VehicleModelsVehicleModelID> vehicleModelDetails = null;
            try
            {
                //Get the data set returned from the data provider
                AutoFixDataSet dataSet = base.DataProvider.VehicleModelDetailsByVehicleModelID();

                if (dataSet != null && dataSet.VehicleModelsVehicleModelID != null && dataSet.VehicleModelsVehicleModelID.Rows != null)
                {
                    //Set the returned filled list
                    vehicleModelDetails = FillVehicleModelsVehicleModelIDObjectsByEntities(dataSet.VehicleModelsVehicleModelID);
                }
            }
            catch (Exception eX)
            {
                //log the error here, dont throw the exception
            }
            //Return the vehicle model details list
            return vehicleModelDetails;
        }

        /// <summary>
        /// Gets all the vehicle models by article id
        /// </summary>
        /// <param name="articleID">article id</param>
        /// <returns>the vehicle model details list</returns>
        public List<GetVehicleModelDetailsByArticleID> GetVehicleModelsByArticleID(int articleID)
        {
            //The vehicle model details to be returned
            List<GetVehicleModelDetailsByArticleID> vehicleModelDetails = null;
            try
            {
                //Get the data set returned from the data provider
                AutoFixDataSet dataSet = base.DataProvider.GetVehicleModelDetailsByArticleID(articleID);

                if (dataSet != null && dataSet.GetVehicleDetailsArticleID != null && dataSet.GetVehicleDetailsArticleID.Rows != null)
                {
                    //Set the returned filled list
                    vehicleModelDetails = FillGetVehicleModelDetailsByArticleIDObjectsByEntities(dataSet.GetVehicleDetailsArticleID);
                }
            }
            catch (Exception eX)
            {
                //log the error here, dont throw the exception
            }
            //Return the vehicle model details list
            return vehicleModelDetails;
        }

        #endregion

        #region [Private Methods]
        /// <summary>
        /// The method that gets the list of vehicle models or just one vehicle model
        /// </summary>
        /// <param name="vehicleModelId">vehicle model id</param>
        /// <param name="selectById">The check to see if select by id or not</param>
        /// <returns>vehicle model list</returns>
        private List<VehicleModel> GetVehicleModels(int vehicleModelId, bool selectById)
        {
            //The vehicle model details to be returned
            List<VehicleModel> vehicleModelDetails = null;
            try
            {
                //Get the data set returned from the data provider
                AutoFixDataSet dataSet = base.DataProvider.GetVehicleModelDetails(vehicleModelId, selectById);

                if (dataSet != null && dataSet.VehicleModel != null && dataSet.VehicleModel.Rows != null)
                {
                    //Set the returned filled list
                    vehicleModelDetails = FillVehicleModelObjectsByEntities(dataSet.VehicleModel);
                }
            }
            catch (Exception eX)
            {
                //log the error here, dont throw the exception
            }
            //Return the vehicle model details list
            return vehicleModelDetails;

        }
        /// <summary>
        /// <summary>
        /// Fill the vehicle model vehicle model id object
        /// </summary>
        /// <param name="dataTable">data table</param>
        /// <returns>the vehicle model vehicle model id list</returns>
        private List<VehicleModelsVehicleModelID> FillVehicleModelsVehicleModelIDObjectsByEntities(AutoFixDataSet.VehicleModelsVehicleModelIDDataTable dataTable)
        {
            //The vehicle model list that contains the objects
            //Loop through the data table and create new VehicleModelsVehicleModelIDObject 
            //finally select the VehicleModelsVehicleModelIDObject and convert to list
            List<VehicleModelsVehicleModelID> vehicleModelsVehicleModelIDDetails = (from vmvmido in
                                                                                        (from vmvmid in dataTable.AsEnumerable<AutoFixDataSet.VehicleModelsVehicleModelIDRow>()
                                                                                         select new
                                                                                         {
                                                                                             VehicleModelsVehicleModelIDObject = new VehicleModelsVehicleModelID()
                                                                                             {
                                                                                                 VehicleModelName = vmvmid.VehicleModelName,
                                                                                                 VehicleModelTypeName = vmvmid.VehicleModelTypeName,
                                                                                                 VehicleYear = vmvmid.VehicleYear,
                                                                                                 VehicleModelTypeImageURL = vmvmid.VehicleModelTypeImageURL,
                                                                                                 VehicleModelTypeID = vmvmid.VehicleModelTypeID,
                                                                                                 VehicleModelTypeYearID = vmvmid.VehicleModelTypeYearID,
                                                                                                 VehicleModelID = vmvmid.VehicleModelID,
                                                                                                 IsActive = vmvmid.IsActive
                                                                                             }
                                                                                         })
                                                                                    select vmvmido.VehicleModelsVehicleModelIDObject).ToList();
            //Return the filled vehicle model vehicle model id details list
            return vehicleModelsVehicleModelIDDetails;
        }

        /// <summary>
        /// <summary>
        /// Fill the vehicle model object
        /// </summary>
        /// <param name="dataTable">data table</param>
        /// <returns>the vehicle model list</returns>
        private List<VehicleModel> FillVehicleModelObjectsByEntities(AutoFixDataSet.VehicleModelDataTable dataTable)
        {
            //The vehicle model list that contains the objects
            //Loop through the data table and create new VehicleModelObject 
            //finally select the VehicleModelObject and convert to list
            List<VehicleModel> vehicleModelDetails = (from vo in
                                                          (from vm in dataTable.AsEnumerable<AutoFixDataSet.VehicleModelRow>()
                                                           select new
                                                           {
                                                               VehicleObject = new VehicleModel()
                                                               {
                                                                   VehicleModelID = vm.VehicleModelID,
                                                                   VehicleModelName = vm.VehicleModelName,
                                                                   IsActive = vm.IsActive
                                                               }
                                                           })
                                                      select vo.VehicleObject).ToList();
            //Return the filled vehicle model details list
            return vehicleModelDetails;
        }

        /// <summary>
        /// <summary>
        /// Fill the vehicle model object
        /// </summary>
        /// <param name="dataTable">data table</param>
        /// <returns>the vehicle model details list</returns>
        private List<GetVehicleModelDetailsByArticleID> FillGetVehicleModelDetailsByArticleIDObjectsByEntities(AutoFixDataSet.GetVehicleDetailsArticleIDDataTable dataTable)
        {
            //The vehicle model list that contains the objects
            //Loop through the data table and create new VehicleModelDetailsObject 
            //finally select the VehicleModelDetailsObject and convert to list
            List<GetVehicleModelDetailsByArticleID> vehicleModelDetails = (from vmdo in
                                                                               (from vmd in dataTable.AsEnumerable<AutoFixDataSet.GetVehicleDetailsArticleIDRow>()
                                                                                select new
                                                                                {
                                                                                    VehicleModelDetailsObject = new GetVehicleModelDetailsByArticleID()
                                                                                    {
                                                                                        VehicleModelName = vmd.VehicleModelName,
                                                                                        VehicleModelTypeName = vmd.VehicleModelTypeName,
                                                                                        VehicleYear = vmd.VehicleYear,
                                                                                        VehicleModelTypeImageURL = vmd.VehicleModelTypeImageURL,
                                                                                        VehicleModelID = vmd.VehicleModelID,
                                                                                        VehicleModelTypeID = vmd.VehicleModelTypeID,
                                                                                        VehicleModelTypeYearID = vmd.VehicleModelTypeYearID
                                                                                    }
                                                                                })
                                                                           select vmdo.VehicleModelDetailsObject).ToList();
            //Return the filled vehicle model details list
            return vehicleModelDetails;
        }
        #endregion
    }
}
